winbrew_core\fs\archive/
cleanup.rs1use std::path::{Path, PathBuf};
2
3use crate::fs::cleanup_path;
4use tracing::warn;
5
6pub(crate) struct ExtractionCleanup {
7 created_files: Vec<PathBuf>,
8 created_dirs: Vec<PathBuf>,
9}
10
11impl ExtractionCleanup {
12 pub(crate) fn new() -> Self {
13 Self {
14 created_files: Vec::new(),
15 created_dirs: Vec::new(),
16 }
17 }
18
19 pub(crate) fn record_file(&mut self, path: PathBuf) {
20 self.created_files.push(path);
21 }
22
23 pub(crate) fn record_directory(&mut self, path: PathBuf) {
24 self.created_dirs.push(path);
25 }
26
27 pub(crate) fn commit(mut self) {
28 self.created_files.clear();
29 self.created_dirs.clear();
30 }
31
32 fn cleanup_recorded_path(path: &Path) {
33 if let Err(err) = cleanup_path(path) {
34 warn!(path = %path.display(), error = %err, "cleanup failed");
35 }
36 }
37}
38
39impl Drop for ExtractionCleanup {
40 fn drop(&mut self) {
41 for path in self.created_files.iter().rev() {
42 Self::cleanup_recorded_path(path);
43 }
44
45 for path in self.created_dirs.iter().rev() {
46 Self::cleanup_recorded_path(path);
47 }
48 }
49}